package com.example.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.book.entity.Book;
import com.example.book.entity.Recommend;
import com.example.book.mapper.RecommendMapper;
import com.example.book.service.IBookService;
import com.example.book.service.IRecommendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.dtos.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 凡哥哥
 * @since 2023-11-06
 */
@Service
@Transactional
public class RecommendServiceImpl extends ServiceImpl<RecommendMapper, Recommend> implements IRecommendService {
    @Autowired
    private IRecommendService recommendService;
    @Autowired
    private RecommendMapper recommendMapper;
    @Autowired
    private IBookService bookService;
    /**
     * 查询图书类型
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public ResponseResult<Page> pageInfo(Integer page, Integer pageSize, String name) {
        Page<Recommend> page1 = new Page(page, pageSize);
        LambdaQueryWrapper<Recommend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Recommend::getName, name);
        Page<Recommend> RecommendPage = this.page(page1, queryWrapper);
        return ResponseResult.okResult(RecommendPage);
    }

    /**
     * 推荐榜图书详细查询
     *
     * @return
     */
    @Override
    public ResponseResult<Page> recommendAll(Integer page, Integer pageSize) {
        Long[] bookIds = recommendMapper.selectAllBookIds();
        return ResponseResult.okResult(bookService.bookIdList(bookIds));
    }

    /**
     * 添加图书推荐
     *
     * @param book
     * @return
     */
    @Override
    public ResponseResult addRecommend(Book book) {
        Recommend recommend = new Recommend();
        BeanUtils.copyProperties(book, recommend, "description");
        recommend.setBookId(book.getId());
        boolean save = recommendService.save(recommend);
        if (save){
            return ResponseResult.okResult("图书推荐成功");
        }
        return ResponseResult.errorResult(500,"图书推荐失败");
    }
    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public ResponseResult deleteRecommend(int[] ids) {
        for (int id : ids) {
            this.removeById(id);
        }
        return ResponseResult.okResult("图书推荐删除成功");
    }

}
